# -*- coding: utf-8 -*-

import random
import time
import sys  # 导入 sys 模块

class SugarReductionApp:
    def __init__(self):
        # Initialize variables
        self.juice = ""
        self.fructose = 0.0
        self.reduced_fructose = 0.0
        self.reduction_time = 0
        self.height = 0.0
        self.weight = 0.0
        self.gender = ""
        self.bmi = 0.0

    def calculate_bmi(self):
        # Calculate BMI
        try:
            height_m = self.height / 100  # Convert to meters
            self.bmi = self.weight / (height_m ** 2)
            print("BMI: {:.2f}".format(self.bmi))
            sys.stdout.flush()  # 强制刷新缓冲区
        except ValueError:
            print("Invalid input")
            sys.stdout.flush()  # 强制刷新缓冲区

    def add_to_log(self):
        # Add data to log
        log_entry = (
            "Juice type: {}, "
            "Fructose content: {}g, "
            "Fructose content after reduction: {}g, "
            "Sugar reduction time: {} minutes, "
            "Height: {}cm, "
            "Weight: {}kg, "
            "Gender: {}, "
            "BMI: {:.2f}\n".format(
                self.juice, self.fructose, self.reduced_fructose, self.reduction_time,
                self.height, self.weight, self.gender, self.bmi
            )
        )
        print("\nLog entry:")
        print(log_entry)
        sys.stdout.flush()  # 强制刷新缓冲区

    def generate_random_data(self):
        # Generate random data
        fruits = ["Hami Melon", "Apple", "Strawberry", "Blueberry", "Banana", "Pear", "Peach", "Lemon", "Mulberry", "Grape", "Orange", "Pineapple", "Kiwi", "Watermelon", "Dragon Fruit", "Mango", "Lychee", "Waxberry"]
        for _ in range(1):  # 每次生成 1 条随机数据
            # Generate random data
            self.juice = random.choice(fruits)
            self.fructose = random.uniform(8, 18)
            self.height = random.randint(155, 190)
            self.weight = random.randint(40, 100)
            self.gender = random.choice(["Male", "Female"])
            self.reduction_time = random.randint(60, 120)  # 1 to 2 hours
            reduced_fructose_percentage = random.uniform(0.55, 0.70)
            self.reduced_fructose = self.fructose * reduced_fructose_percentage

            # Calculate BMI
            self.calculate_bmi()

            # Create log entry
            log_entry = (
                "Juice type: {}, "
                "Fructose content: {:.2f}g, "
                "Fructose content after reduction: {:.2f}g, "
                "Sugar reduction time: {} minutes, "
                "Height: {}cm, "
                "Weight: {}kg, "
                "Gender: {}, "
                "BMI: {:.2f}\n".format(
                    self.juice, self.fructose, self.reduced_fructose, self.reduction_time,
                    self.height, self.weight, self.gender, self.bmi
                )
            )
            print("\nRandom log entry:")
            print(log_entry)
            sys.stdout.flush()  # 强制刷新缓冲区

def main():
    app = SugarReductionApp()
    interval = 5  # 每隔 5 秒生成一次数据
    while True:
        app.generate_random_data()  # 生成随机数据
        time.sleep(interval)  # 等待指定时间

if __name__ == "__main__":
    main()
